class code4 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //避免删头节点，引入傀儡节点
        ListNode dummy = new ListNode(0,head);
        Stack<ListNode> stack = new Stack<>();

        ListNode cur = dummy;
        while(cur!=null) {
            stack.push(cur);
            cur = cur.next;
        }

        while(n--!=0) {
            stack.pop();
        }
        ListNode prev = stack.peek();
        prev.next = prev.next.next;

        return dummy.next;
    }
}